﻿namespace ConfigDatabase.Forms
{
    using ConfigDatabase.Class;
    using Npgsql;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;
    //using UCcomponent.Compo;
    public class frmchondatabase : Form
    {
        private Button btnsave;
        private Button butconnect;
        private Button butexit;
        private IContainer components = null;
        private ConnectDatabase conndata = new ConnectDatabase();
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private Label label6;
        private Label label8;
        private TextBox schema;
        private TextBox txtdatabase;
        private TextBox txtpassword;
        private TextBox txtport;
        private TextBox txtserver;
        private TextBox txtuserid;

        public frmchondatabase()
        {
            this.InitializeComponent();
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                int num = int.Parse(this.txtport.Text);
            }
            catch
            {
                MessageBox.Show("Port number is incorrect format.\nPlease try again.", "cvsoft", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            this.conndata.IpAdress = this.txtserver.Text;
            this.conndata.Port = int.Parse(this.txtport.Text);
            this.conndata.User = this.txtuserid.Text;
            this.conndata.PassWord = this.txtpassword.Text;
            this.conndata.Database = this.txtdatabase.Text;
            this.conndata.Schema = this.schema.Text;
            this.conndata.SaveFileConnect();
            base.Close();
        }

        private void butconnect_Click(object sender, EventArgs e)
        {
            string connectionString = "Server=" + this.txtserver.Text + ";Port=" + this.txtport.Text + ";User Id=" + this.txtuserid.Text + ";Password=" + this.txtpassword.Text + ";Database=" + this.txtdatabase.Text + ";Encoding=UNICODE;Pooling=true;";
            DataTable dataTable = new DataTable();
            NpgsqlConnection selectConnection = new NpgsqlConnection(connectionString);
            try
            {
                selectConnection.Open();
                new NpgsqlDataAdapter("select * from pg_catalog.pg_user", selectConnection).Fill(dataTable);
                if (dataTable == null)
                {
                    if (MessageBox.Show("Cannot connect to Server\nDo you want restore default connection?", "cvsoft", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        File.Delete(this.conndata.PathConnect);
                        this.conndata = new ConnectDatabase();
                        this.frmchondatabase_Load(null, null);
                    }
                }
                else
                {
                    MessageBox.Show("Connected to Server is successful!", "cvsoft", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError("Message: " + exception.Message, "connection", "frmchondatabase.cs");
                if (MessageBox.Show("Cannot connect to Server\nDo you want restore default connection?", "cvsoft", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    File.Delete(this.conndata.PathConnect);
                    this.conndata = new ConnectDatabase();
                    this.frmchondatabase_Load(null, null);
                }
            }
            finally
            {
                selectConnection.Close();
                selectConnection.Dispose();
            }
        }

        private void butexit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void EnableControl(bool b)
        {
            this.txtuserid.Enabled = this.txtpassword.Enabled = this.schema.Enabled = b;
        }

        private void frmchondatabase_Load(object sender, EventArgs e)
        {
            
            this.txtserver.Text = this.conndata.IpAdress;
            this.txtport.Text = this.conndata.Port.ToString();
            this.txtuserid.Text = this.conndata.User;
            this.txtpassword.Text = this.conndata.PassWord;
            this.txtdatabase.Text = this.conndata.Database;
            this.schema.Text = this.conndata.Schema;
            this.EnableControl(false);
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmchondatabase));
            this.label1 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.txtserver = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtdatabase = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.txtport = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.txtuserid = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.txtpassword = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.schema = new System.Windows.Forms.TextBox();
            this.btnsave = new System.Windows.Forms.Button();
            this.butexit = new System.Windows.Forms.Button();
            this.butconnect = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(34, 60);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(94, 17);
            this.label1.TabIndex = 0;
            this.label1.Text = "IP ADDRESS:";
            // 
            // label3
            // 
            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.label3.BackColor = System.Drawing.Color.Teal;
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 22F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.ForeColor = System.Drawing.Color.Yellow;
            this.label3.Location = new System.Drawing.Point(0, 1);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(456, 46);
            this.label3.TabIndex = 1;
            this.label3.Text = "SELECT DATABASE";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // txtserver
            // 
            this.txtserver.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtserver.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtserver.Location = new System.Drawing.Point(137, 59);
            this.txtserver.Name = "txtserver";
            this.txtserver.Size = new System.Drawing.Size(306, 21);
            this.txtserver.TabIndex = 0;
            this.txtserver.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtserver_KeyDown);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(43, 86);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(85, 17);
            this.label2.TabIndex = 0;
            this.label2.Text = "DATABASE:";
            // 
            // txtdatabase
            // 
            this.txtdatabase.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtdatabase.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtdatabase.Location = new System.Drawing.Point(137, 85);
            this.txtdatabase.Name = "txtdatabase";
            this.txtdatabase.Size = new System.Drawing.Size(306, 21);
            this.txtdatabase.TabIndex = 1;
            this.txtdatabase.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtserver_KeyDown);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label4.Location = new System.Drawing.Point(75, 111);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(51, 17);
            this.label4.TabIndex = 0;
            this.label4.Text = "PORT:";
            // 
            // txtport
            // 
            this.txtport.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtport.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtport.Location = new System.Drawing.Point(137, 110);
            this.txtport.Name = "txtport";
            this.txtport.Size = new System.Drawing.Size(306, 21);
            this.txtport.TabIndex = 2;
            this.txtport.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtserver_KeyDown);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.Location = new System.Drawing.Point(64, 161);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(63, 17);
            this.label5.TabIndex = 0;
            this.label5.Text = "USERID:";
            // 
            // txtuserid
            // 
            this.txtuserid.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtuserid.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtuserid.Location = new System.Drawing.Point(137, 160);
            this.txtuserid.Name = "txtuserid";
            this.txtuserid.Size = new System.Drawing.Size(306, 21);
            this.txtuserid.TabIndex = 4;
            this.txtuserid.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtserver_KeyDown);
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label6.Location = new System.Drawing.Point(35, 187);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(92, 17);
            this.label6.TabIndex = 0;
            this.label6.Text = "PASSWORD:";
            // 
            // txtpassword
            // 
            this.txtpassword.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtpassword.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtpassword.Location = new System.Drawing.Point(137, 186);
            this.txtpassword.Name = "txtpassword";
            this.txtpassword.PasswordChar = '*';
            this.txtpassword.Size = new System.Drawing.Size(306, 21);
            this.txtpassword.TabIndex = 5;
            this.txtpassword.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtserver_KeyDown);
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label8.Location = new System.Drawing.Point(58, 136);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(69, 17);
            this.label8.TabIndex = 0;
            this.label8.Text = "SCHEMA:";
            // 
            // schema
            // 
            this.schema.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.schema.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.schema.Location = new System.Drawing.Point(137, 136);
            this.schema.Name = "schema";
            this.schema.Size = new System.Drawing.Size(306, 21);
            this.schema.TabIndex = 3;
            this.schema.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtserver_KeyDown);
            // 
            // btnsave
            // 
            this.btnsave.Image = ((System.Drawing.Image)(resources.GetObject("btnsave.Image")));
            
            this.btnsave.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnsave.Location = new System.Drawing.Point(230, 231);
            this.btnsave.Name = "btnsave";
            this.btnsave.Size = new System.Drawing.Size(75, 25);
            this.btnsave.TabIndex = 8;
            this.btnsave.Text = "     SAVE";
            this.btnsave.UseVisualStyleBackColor = true;
            this.btnsave.Click += new System.EventHandler(this.btnsave_Click);
            // 
            // butexit
            // 
            this.butexit.Image = ((System.Drawing.Image)(resources.GetObject("butexit.Image")));
            this.butexit.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butexit.Location = new System.Drawing.Point(310, 231);
            this.butexit.Name = "butexit";
            this.butexit.Size = new System.Drawing.Size(75, 25);
            this.butexit.TabIndex = 10;
            this.butexit.Text = "           EXIT";
            this.butexit.UseVisualStyleBackColor = true;
            this.butexit.Click += new System.EventHandler(this.butexit_Click);
            // 
            // butconnect
            // 
            this.butconnect.Image = ((System.Drawing.Image)(resources.GetObject("butconnect.Image")));
            this.butconnect.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butconnect.Location = new System.Drawing.Point(137, 231);
            this.butconnect.Name = "butconnect";
            this.butconnect.Size = new System.Drawing.Size(89, 25);
            this.butconnect.TabIndex = 7;
            this.butconnect.Text = "       CONNECT";
            this.butconnect.UseVisualStyleBackColor = true;
            this.butconnect.Click += new System.EventHandler(this.butconnect_Click);
            // 
            // frmchondatabase
            // 
            this.ClientSize = new System.Drawing.Size(455, 283);
            this.Controls.Add(this.butexit);
            this.Controls.Add(this.butconnect);
            this.Controls.Add(this.btnsave);
            this.Controls.Add(this.schema);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.txtpassword);
            this.Controls.Add(this.txtuserid);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.txtport);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.txtdatabase);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.txtserver);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmchondatabase";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Load += new System.EventHandler(this.frmchondatabase_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void txtserver_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
            if (((e.Alt && e.Control) && e.Shift) && (e.KeyCode == Keys.A))
            {
                frmSuaThongSo_pass _pass = new frmSuaThongSo_pass();
                _pass.ShowDialog();
                if (_pass.ThanhCong)
                {
                    this.EnableControl(true);
                }
            }
        }
    }
}

